package com.room2.ui;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.room2.R;
import com.room2.adapter.MyScoreAdapter;
import com.room2.model.Score;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScoreFragment extends Fragment {

    private ScoreViewModel viewModel;
    private MyScoreAdapter adapter;

    public ScoreFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_score, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String[] name = {
                "你好",
                "世界",
                "安卓系统",
                "谷歌公司",
                "工作室",
                "项目",
                "数据库",
                "回收站",
                "视图",
                "字符串",
                "价值",
                "整数类型"
        };
        double[] score = {
                11,
                12,
                13,
                14,
                15,
                16,
                17,
                18,
                19,
                20,
                21,
                22
        };

        Button btn_add = getView().findViewById(R.id.btn_add);
        Button btn_clear = getView().findViewById(R.id.btn_clear);

        viewModel = ViewModelProviders.of(getActivity()).get(ScoreViewModel.class);
        for (int i = 0; i < name.length; i++) {
            viewModel.insert(new Score(name[i], score[i]));
        }
        adapter = new MyScoreAdapter(viewModel);
        RecyclerView recyclerview = getView().findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview.setAdapter(adapter);

        viewModel.getAllData().observe(getActivity(), new Observer<List<Score>>() {
            @Override
            public void onChanged(List<Score> scores) {
                adapter.setList(scores);
                adapter.notifyDataSetChanged();
            }
        });


        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.insert(new Score("数学", 199));
            }
        });
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.clear();
            }
        });
    }
}
